$(document).ready(function(){
  //
  $('#loginButton').click(function(){
    $('#userName').focus();
  });
  //为登录按钮绑定事件
  $('#login').click(function(){
		if($('#userName').val().trim()=='' || $('#password').val().trim()==''){
			$("#loginBox .tip").html("userName or passoword is not empty");
			return;
		}
		$("#loginBox .tip").html("");
		$.ajax({
				url:'CenterController',
				data:{
					'operator':'login',
					'userName':$('#userName').val(),
					'password':$('#password').val()
				},
				dataType:'JSON',
				success:function(result){
							if(result.success){
								var time=0;
								if($('#checkbox').is(':checked')){
										time=7*24*60*60*1000;
								}
								setCookie("loginInfo",$('#userName').val().trim(),time);
								$('#loginForm').html('<div class="messageinfo" style="width:300px;">登录成功！</div>');
								setTimeout(function(){
										window.location.href="index.html";
								},1000);
							}else{
								$("#loginBox .tip").html(result.tip);
							}
				}
		});
	});

  $("body").on("click",".img-responsive",function(){
	  	
		
		var value=$(this).next("input[type='hidden']").val();
		var num=getCookie("num");
		num="-1";
		//console.log(value);
		$.ajax({
			url:"CenterController",
			data:{"operator":"single","id":value,"num":num},
			dataType:"text",
			success:function(result){
				window.location.href="show.html";
			}
		});
		
	});
  
  $('#personal').click(function(){
	  console.log($(this).html());
	  $.ajax({
		  url:"CenterController",
		  data:{"operator":"personal","userName":$(this).html()},
		  dataType:"json",
		  success:function(result){
			  if(result.success){
				  window.location.href="personal.html";
			  }else{
				  window.location.href="index.html";
			  }
		  }
	  });
	});
  //为退出绑定事件
	$('#exit').click(function(){
		delCookie("loginInfo");
		location.href="index.html";
	});
	

});

function setCookie(name,value,time){
  var exp=new Date();
  exp.setTime(exp.getTime()+time);
  document.cookie=name+"="+encodeURIComponent(value)+";"+"expires="+exp.toGMTString();
}
function getCookie(name){
  if(document.cookie.length>0){
    var start=document.cookie.indexOf(name+"=");
    if(start!=-1){
      start=start+name.length+1;
      var end=document.cookie.indexOf(";",start);
      if(end==-1){
    	  end=document.cookie.length;
      }
      return decodeURIComponent(document.cookie.substring(start,end));
    }
  }
  return "";
}
function delCookie(name) {
  var exp = new Date();
  exp.setTime(exp.getTime() + (-1 * 24 * 60 * 60 * 1000));
  var cval = getCookie(name);
  document.cookie = name + "=" + encodeURIComponent(cval) + "; expires=" + exp.toGMTString();
}
function choose(obj){
  var albumId=$(obj).find("input").val();
  $.ajax({
    url:"CenterController",
    data:{"operator":"categroy","albumId":albumId},
    dataType:"JSON",
    success:function(result){
    	$('.grid_1').css("display","none");
      if(result.length==0){
        $(".stock_box").css("display","block");
        $(".pagination").css("display","none");
        $(".stock_box").html('<div>在该查询条件下，没有内容显示</div>').addClass('messageinfo');
        return;
      }else{
        $('.banner').css("display","none");
        $(".tab_img").html("");
        for (var i = 0; i < result.length; i++) {
          var $img=$("<img src='images/"+result[i].path+"' class='img-responsive' alt=''/>");
          var $p=$("<p>"+result[i].name+"</p>");
          var $h4=$("<h4>#"+result[i].praise+"</h4>");
          var $div=$("<div class='tab_desc'></div>");
          $div.append($p).append($h4);
          var $a=$("<a href='javascript:;'></a>");
          var $input=$("<input type='hidden' class='hidden' value='"+result[i].id+"'>");
          $a.append($img).append($input).append($div);
          var $li=$("<li></li>");
          $li.append($a);
          $(".tab_img").append($li);
        }
        if(result.length<36){
          $(".pagination").css("display","none");
        }
        $(".stock_box").css("display","block");
      }
    }
  });
}
function showLogin(){
	$("#loginBox").css("display","block");
}
function hideLogin(){
	$("#loginBox").css("display","none");
}